package com.deer.wms.system.manage.web;

import com.deer.wms.busine.tactic.model.Upper.UpperTactic;
import com.deer.wms.busine.tactic.service.UpperTacticService;
import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.model.address.ShippingAddressCriteria;
import com.deer.wms.system.manage.model.address.ShippingAddressDto;
import com.deer.wms.system.manage.model.user.UserInfoDto;
import com.deer.wms.system.manage.model.ware.WareInfo;
import com.deer.wms.system.manage.model.ware.WareInfoCriteria;
import com.deer.wms.system.manage.model.ware.WareInfoDto;
import com.deer.wms.system.manage.model.ware.WareInfoDto2;
import com.deer.wms.system.manage.service.ShippingAddressService;
import com.deer.wms.system.manage.service.UserInfoService;
import com.deer.wms.system.manage.service.WareInfoService;
import com.deer.wms.system.manage.service.WareUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
*
 * @author guo
 * @since 2019/11/06
 */
@Api(description = "仓库管理api接口")
@RestController
@RequestMapping("/ware/infos")
public class WareInfoController {

    @Autowired
    private WareInfoService wareInfoService;

    @Autowired
    private WareUserService wareUserService;

    @Autowired
    private UpperTacticService upperTacticService;

    @Autowired
    private ShippingAddressService shippingAddressService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoService userInfoService;



    @PostMapping("/insert")
    @ApiOperation(value = "添加仓库信息", notes = "添加仓库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    public Result add(@RequestBody WareInfoDto wareInfo,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        WareInfo wareInfo1 = null;
        List<WareInfoDto2> list = wareInfoService.findByName(wareInfo.getWareName());
        List<WareInfo> list2 = wareInfoService.getListByField("ware_code", wareInfo.getWareCode());
        if(list.size() == 0 && list2.size() == 0){
            wareInfo.setDutyUserId(currentUser.getUserId());
            wareInfo1 = wareInfoService.createWare(wareInfo);
        }else {
            throw new ServiceException(CommonCode.WARE_EXISTS, "此仓库已存在");
        }
        return ResultGenerator.genSuccessResult(wareInfo1);
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/delete")
    @ApiOperation(value = "删除仓库信息", notes = "删除仓库信息")

    public Result delete(Integer wareId, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR, "未登录！", null);
        }
        WareInfoCriteria criteria = new WareInfoCriteria();
        criteria.setWareId(wareId);
        ShippingAddressCriteria criteria1 = new ShippingAddressCriteria();
        criteria1.setWareId(wareId);
        List<ShippingAddressDto> list1 = shippingAddressService.findList(criteria1);
        if(list1.size() != 0){
            throw new ServiceException(CommonCode.WARE_SHIPPADDRESS_BOUND, "此仓库已绑定发货地址");
        }

        List<UpperTactic> list = upperTacticService.findByWareId(wareId);
        if(list.size() != 0){
            throw new ServiceException(CommonCode.WARE_UPPER_TACTIC_BOUND, "此仓库已绑定上架策略");
        }
            wareInfoService.deleteByCodeAndCom(criteria);
            wareUserService.deleteByWareId(wareId);
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @PostMapping("/update")
    @ApiOperation(value = "更新仓库信息", notes = "更新仓库信息")
    public Result update(@RequestBody WareInfo wareInfo,@ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        wareInfoService.updateById(wareInfo);
//    /**
//     * 保存仓库保管员列表
//     */
//        wareUserService.deleteByWareId(wareInfo.getWareId());
//        Integer wareId = wareInfo.getWareId();
//        WareUser wareUser = new WareUser();
//        wareUser.setWareId(wareId);
//        wareUser.setCompanyId(wareInfo.getCompanyId());
//        List<Integer> userIds = wareInfoDto.getWareUserIds();
//        if (userIds != null && userIds.size() > 0) {
//            for (Integer userId : userIds) {
//                wareUser.setUserId(userId);
//                wareUser.setWareId(wareInfo.getWareId());
//                wareUser.setWareUserId(null);
//                wareUserService.save(wareUser);
//            }
//        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    @ApiOperation(value = "单个仓库查询", notes = "单个仓库查询")
    public Result detail(Integer id) {
        WareInfo wareInfo = wareInfoService.getById(id);
        return ResultGenerator.genSuccessResult(wareInfo);
    }


    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/list")
        public Result list(WareInfoCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        if(currentUser.getIsAdmin()!=1){
            criteria.setUserId(currentUser.getUserId());
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<WareInfoDto>list = wareInfoService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);

       /* if(currentUser.getIsAdmin() ==1){
            PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
            List<WareInfoDto2>list = wareInfoService.findSome(criteria);
            PageInfo pageInfo = new PageInfo(list);
            return ResultGenerator.genSuccessResult(pageInfo);
        }else {
            criteria.setUserId(currentUser.getUserId());
            PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
            List<WareInfoDto>list = wareInfoService.findList(criteria);
            PageInfo pageInfo = new PageInfo(list);
            return ResultGenerator.genSuccessResult(pageInfo);
        }*/
    }

    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/findSome")
    public Result findSome(WareInfoCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<WareInfoDto2> list = wareInfoService.findSome(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/findUserByWareId")
    public Result findUserByWareId(Integer wareId, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<UserInfoDto> list = userInfoService.findUserByWareId(wareId);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Authority
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/allList")
    public Result allList( ) {
       /* if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }*/

        List<WareInfo> list=null ;//= (List<WareInfo>) redisUtil.get("allWareInfo");
        if(list==null || list.size()==0){
            list = wareInfoService.list();
            redisUtil.set("allWareInfo",list);
        }

       // PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(list);
    }

   /* @GetMapping("/select")
    @ApiOperation(value = "查询编码名称是否存在", notes = "查询编码名称是否存在")
    public Result select(InsertData insertData) {
        //判断仓库名称是否存在
        if (insertData.getWareName() != null) {
            List<WareInfoDto2> list = wareInfoService.findByName(insertData.getWareName());
            if (list.size() == 0) {
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS );
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断物料名称是否存在
        if (insertData.getItemName() != null) {
            List<ItemInfo> list = itemInfoService.findByName(insertData.getItemName());
            if (list.size() == 0) {
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断物料类型名称、编码是否存在
        if(insertData.getItemTypeName() !=null){
            List<ItemType> list = itemTypeService.findByName(insertData.getItemTypeName());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getItemTypeCode() != null){
            List<ItemType> list = itemTypeService.findByCode(insertData.getItemTypeCode());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断包装代码、描述是否存在
        if(insertData.getPackCode() != null){
            List<PackMasterDto> list = packMasterService.findByPackCode(insertData.getPackCode());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getPackName() != null){
            List<PackMasterDto> list = packMasterService.findByPackName(insertData.getPackName());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断包材名称是否存在
        if(insertData.getPackData() != null){
            List<Pack> list = packService.findByPackData(insertData.getPackData());
            if(list.size() ==0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断供应商名称、编码是否存在
        if(insertData.getSupplierCode() != null){
            List<SupplierManageDto> list = supplierManageService.findByCode(insertData.getSupplierCode());
            if(list.size() ==0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getSupplierName() != null){
            List<SupplierManageDto> list = supplierManageService.findByName(insertData.getSupplierName());
            if(list.size() ==0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断货主名称、编码是否存在
        if(insertData.getSupplierCode() != null){
            List<OrganizationDto> list = organizationService.findByOrganizationCode(insertData.getOrganizationCode());
            if(list.size() ==0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getSupplierName() != null){
            List<OrganizationDto> list = organizationService.findByOrganizationName(insertData.getOrganizationName());
            if(list.size() ==0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断客户名称、编码是否存在
        if(insertData.getCustomerCode() != null){
            List<CustomerDto> list = customerService.findByCustomerCode(insertData.getCustomerCode());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getCustomerName() != null){
            List<CustomerDto> list = customerService.findByCustomerName(insertData.getCustomerName());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断承运方名称、编码是否存在
        if(insertData.getCarrierCode() != null){
            List<Carrier> list = carrierService.findByCode(insertData.getCarrierCode());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getCarrierName() != null){
            List<Carrier> list = carrierService.findByName(insertData.getCarrierName());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断部门名称、编码是否存在
        if(insertData.getDeptCode() != null){
            List<DeptDto> list = deptService.findByDeptCode(insertData.getDeptCode());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        if(insertData.getDeptName() != null){
            List<DeptDto> list = deptService.findByDeptName(insertData.getDeptName());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断用户名称是否存在
        if(insertData.getAccount() != null){
            List<UserInfo> list = userInfoService.findByAccount(insertData.getAccount());
            if(list.size() == 0){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
        }
        //判断角色名称是否存在
        if(insertData.getRoleName() != null){
                throw new ServiceException(CommonCode.NAME_CODE_NO_EXISTS);
            } else {
                throw new ServiceException(CommonCode.NAME_CODE_EXISTS);
            }
    }*/
}
